package com.it.service.impl;

import com.it.VO.CommentVo;
import com.it.common.Result;
import com.it.dao.CommentMapper;
import com.it.dao.UserMapper;
import com.it.domain.Comment;
import com.it.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result SaveComment(Comment comment) {
        if(commentMapper.SaveComment(comment)>0){
            return Result.success("200","评论成功");
        }
        else{
            return Result.fail("504","评论失败");
        }
    }

    @Override
    public List<CommentVo> getMainComment(Integer commType, Integer commTargetId) {

        List<Comment> comments = commentMapper.getMainComment(commType,commTargetId);
        List<CommentVo> commentVos = new ArrayList<>();
        for(Comment comment : comments){
            CommentVo commentVo = new CommentVo();
            commentVo.setContent(comment.getContent());
            commentVo.setUsername(comment.getUser().getUsername());
            commentVo.setId(comment.getId());
            commentVo.setImage(comment.getUser().getImage());
            commentVo.setUserId(comment.getUserId());
            commentVo.setParentId(comment.getParentId());
            commentVo.setRootId(comment.getRootId());
            commentVo.setTime(comment.getCommTime());
            if(comment.getParentId() != -1){
                commentVo.setToCommentUSerName(userMapper.SelectById(comment.getToCommUserId()).getUsername());
            }
            System.out.println(commentVo.toString());
            commentVos.add(commentVo);
        }
        return commentVos;
    }

    @Override
    public Integer DeleteComment(Comment comment) {
        return commentMapper.DeleteComment(comment);
    }

    @Override
    public Integer DeleteRootComment(Comment comment) {
        return commentMapper.DeleteRootComment(comment);
    }

    @Override
    public List<Comment> SelectByParentId(Comment comment) {
        return commentMapper.SelectByParentId(comment);
    }

    @Override
    public List<Comment> SelectRootId(Integer rootId, Integer commTargetId, Integer commType) {
        List<Comment> comments = commentMapper.SelectRootId(rootId,commTargetId,commType);
        if(comments == null){
            return null;
        }
        else{
            return comments;
        }
    }

}
